﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Yu.Cms.Common;
using Yu.Cms.IDAL;

namespace Yu.Cms.SQLServerDAL
{
    public class BigType : IBigType
    {
        private readonly string TableName = "Y_BigType";

        #region IBigType 成员
        /// <summary>
        /// 获取所有栏目数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCount()
        {
            return DbHelperMsSQL.GetMaxCount(TableName, null);
        }
      

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Yu.Cms.Model.BigType model)
        {
           
            int iSort = DbHelperMsSQL.GetMaxID(TableName, "BigTypeSort", null);
           
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + TableName + "(");
            strSql.Append("BigTypeName,BigTypeSort)");
            strSql.Append(" values (");
            strSql.Append("@BigTypeName," + iSort + ")");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@BigTypeName", SqlDbType.NVarChar,50)
					};
            parameters[0].Value = model.BigTypeName;

            object obj = DbHelperMsSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return -1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Yu.Cms.Model.BigType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + TableName + " set ");
            strSql.Append("BigTypeName=@BigTypeName");
           strSql.Append(" where BigTypeID=@BigTypeID ");
            SqlParameter[] parameters = {
					new SqlParameter("@BigTypeID", SqlDbType.Int,4),
					new SqlParameter("@BigTypeName", SqlDbType.NVarChar,50)
				};
            parameters[0].Value = model.BigTypeID;
            parameters[1].Value = model.BigTypeName;
            
            return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="BigTypeID"></param>
        /// <returns></returns>
        public int Delete(int BigTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + TableName);
            strSql.Append(" where BigTypeID=@BigTypeID ");
            SqlParameter[] parameters = {
					new SqlParameter("@BigTypeID", SqlDbType.Int,4)};
            parameters[0].Value = BigTypeID;

            return DbHelperMsSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="BigTypeID"></param>
        /// <returns></returns>
        public Yu.Cms.Model.BigType GetModel(int BigTypeID)
        {
            int RecordCount;
            Yu.Cms.Model.BigType fam = new Yu.Cms.Model.BigType();
            QueryParam qp = new QueryParam();
            qp.Where = " Where BigTypeID=" + BigTypeID;
            IList<Yu.Cms.Model.BigType> lst = this.GetList(qp, out RecordCount);
            if (lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 返回大类列表
        /// </summary>
        /// <returns></returns>
        public IList<Yu.Cms.Model.BigType> GetList()
        {
            int RecordCount;
            return this.GetList(new QueryParam(), out RecordCount);
        }

        
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public bool SortBigType(Yu.Cms.Model.BigType model, Yu.Enum.Sort sort)
        {
            int BigTypeID=model.BigTypeID;
            int BigTypeSort = model.BigTypeSort;
            return DbHelperMsSQL.SwitchSort(TableName,"BigTypeSort",BigTypeSort, "BigTypeID", BigTypeID,null,0, sort);
        }

        /// <summary>
        /// 将记录集转为BigType实体类 (Y_BigType)
        /// </summary>
        /// <param name="dr">记录集</param>
        /// <param name="Fileds">字段名列表</param>
        /// <returns>BigType</returns>
        protected Yu.Cms.Model.BigType Populate_BigType(IDataReader dr, Dictionary<string, string> Fileds)
        {
            Yu.Cms.Model.BigType nc = new Yu.Cms.Model.BigType();
            if (Fileds.ContainsKey("BigTypeID") && !Convert.IsDBNull(dr["BigTypeID"])) nc.BigTypeID = Convert.ToInt32(dr["BigTypeID"]); // 自动ID
            if (Fileds.ContainsKey("BigTypeName") && !Convert.IsDBNull(dr["BigTypeName"])) nc.BigTypeName = Convert.ToString(dr["BigTypeName"]).Trim(); // 大类名称
            if (Fileds.ContainsKey("BigTypeSort") && !Convert.IsDBNull(dr["BigTypeSort"])) nc.BigTypeSort = Convert.ToInt32(dr["BigTypeSort"]); // 排序ID
            return nc;
        }


        /// <summary>
        /// 返回BigType实体类的IList对象 (Y_BigType)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>BigType实体类的IList对象(Y_BigType)</returns>
        protected IList<Yu.Cms.Model.BigType> GetList(QueryParam qp, out int RecordCount)
        {          
            qp.TableName = TableName;
            qp.OrderType = 0;
            qp.Orderfld = "BigTypeSort";            

            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            else
            {
                qp.ReturnFields += ",";
                qp.ReturnFields += qp.Orderfld;
            }
            DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.BigType> mypd = new DbHelperMsSQL.PopulateDelegate<Yu.Cms.Model.BigType>(this.Populate_BigType);
            return DbHelperMsSQL.GetRecordListBySql(mypd, qp, out RecordCount);
        }

        
        #endregion
    }
}
